package com.mes.common;

import com.mes.entity.User;
import com.yy.base.util.SessionInfo;
import com.yy.base.util.UserUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception {
		HttpSession session = request.getSession();
		User user = null;
		if (session != null) {
			user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
			SessionInfo.setUser(user);
		}
//		String uri = request.getRequestURI();
//		if (uri.indexOf("screen") > -1 || uri.indexOf("script") > -1 || uri.indexOf("common") > -1 || uri.indexOf("kanban") > -1) {
//			return;
//		}
//		if (user == null) {
//			response.flushBuffer();
//			response.getWriter().print("登录超时！");
//			response.getWriter().close();
//		}
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		HttpSession session = request.getSession();
		User user = null;
		if (session != null) {
			user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
			SessionInfo.setUser(user);
		}
//		String uri = request.getRequestURI();
//		if (uri.indexOf("screen") > -1 || uri.indexOf("script") > -1 || uri.indexOf("common") > -1) {
//			return true;
//		}
//		if (user == null) {
//			response.flushBuffer();
//			response.getWriter().print("登录超时！");
//			response.getWriter().close();
//			return false;
//		}
//		List<PdaModule> list = user.getModuleList();
//		boolean hasAuth = false;
//		for (PdaModule pdaModule : list) {
//			if (uri.startsWith(request.getContextPath() + pdaModule.getUrl())) {
//				hasAuth = true;
//				break;
//			}
//		}
		return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
	}
}
